package com.proj.hot.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

/**
 * Bitmap工厂，里面提供了一些Bitmap的处理、转换等方法。
 *
 * 
 */
public class BitmapUtils
{
	/**
	 * 为某张Bitmap在下方创建一个倒影
	 * 
	 * @param originalImage 待处理的源图片
	 * @return Bitmap 返回源图片+倒影
	 */
	public static Bitmap createReflectedImage(Bitmap originalImage)
	{
		// 原图与倒影之间的距离
		final int reflectionGap = 4;
		int width = originalImage.getWidth();
		int height = originalImage.getHeight();
		// 创建一个垂直方向翻转的Matrix
		Matrix matrix = new Matrix();
		matrix.preScale(1, -1);
		// 根据这个Matrix生成倒影图
		// 倒影的高度是原图的一半
		Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width,
				height / 2, matrix, false);
		// 创建一个新Bitmap用来盛放原图+倒影图
		Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2),
				Config.ARGB_8888);
		// 创建一张能装下bitmapWithReflection的画布
		Canvas canvas = new Canvas(bitmapWithReflection);
		// 把原图绘制在画布上
		canvas.drawBitmap(originalImage, 0, 0, null);
		Paint defaultPaint = new Paint();
		// 把间距绘制在画布上
		canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
		// 把倒影绘制在画布上
		canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
		// 创建一个线性渐变的蒙版覆盖在倒影上实现虚化效果
		Paint paint = new Paint();
		LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0,
				bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff,
				TileMode.CLAMP);
		// 把shader添加到画笔中
		paint.setShader(shader);
		// 设置PorterDuff模式
		paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
		// 用这个画笔把蒙版绘制到倒影表面
		canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);
		return bitmapWithReflection;
	}
	
	/**
	 * 将drawable 类型转化为bitmap
	 * 
	 * @param Drawable 原始的Drawable类型的图片
	 * @return 转换为Bitmap后的图片
	 */
	public static Bitmap drawableToBitmap(Drawable drawable)
	{
		Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable
				.getIntrinsicHeight(),
				drawable.getOpacity() != PixelFormat.OPAQUE ? Config.ARGB_8888
						: Config.RGB_565);
		Canvas canvas = new Canvas(bitmap);
		// canvas.setBitmap(bitmap);
		drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
		drawable.draw(canvas);
		return bitmap;
	}
	
	/**
	 * @description: <将Bitmap转换成Drawable>
	 * @param context
	 * @param bitmap 需要转换的bitmap
	 * @return 返回的drawable
	 */
	public static Drawable bitmap2Drawable(Context context, Bitmap bitmap)
	{
		BitmapDrawable bd = new BitmapDrawable(context.getResources(), bitmap);
		Drawable d = (Drawable) bd;
		return d;
	}
}
